﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Activities.Rules;
using System.Workflow.Activities.Rules.Design;
using MWTWorkUnitMgmtTool.BusinessEntities;
using System.Windows.Forms;
using System.Workflow.ComponentModel.Serialization;
using System.Xml;

namespace MWTWorkUnitMgmtTool.RulesEngine
{
    public class MappingRulesCreator:IRulesCreator
    {
        #region IRulesCreator Members

        public void CreateNewRuleSet<T>(T t) where T:class,new() 
        {
            RuleSet ruleSet = null;
            string fileName =  t.GetType().Name + ".rules";

            RuleSetDialog ruleSetDialog = new RuleSetDialog(typeof(T), null, ruleSet);
            DialogResult dialogResult = ruleSetDialog.ShowDialog();

            ruleSet = ruleSetDialog.RuleSet;
            if (dialogResult == DialogResult.OK)
            {
                WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

                XmlWriter rulesWriter = XmlWriter.Create(fileName);
                serializer.Serialize(rulesWriter, ruleSet);
                rulesWriter.Close(); 
            }
            

        }

        public string Name()
        {
            return "RulesetCreator"; 
        }

        #endregion

       
    }
}
